import { IEvent } from '../type/IEvent';
import { BNode } from '../type/BNode';

// 转化promise成BNode节点
export class PromiseNode extends BNode implements IEvent {
    constructor(public call: () => Promise<any>) {
        super();
    }
    onStart(blackboard?: any): void {
        this.resolve();
    }
    onCancel(): void {
        this.call = Promise.reject;
    }

    async resolve() {
        await this.call();
        this.task.done();
    }
}

//检测promise是否已经完成
export class WaitPromiseNode extends BNode implements IEvent {
    constructor(public call: Promise<any>) {
        super();
    }
    onStart(blackboard?: any): void {
        this.resolve();
    }
    onCancel(): void {
        this.call = Promise.reject();
    }

    async resolve() {
        await this.call;
        this.task.done();
    }
}
